package com.jeeplus.modules.project.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.project.entity.KnotExpertAvoid;
import com.jeeplus.modules.project.service.KnotExpertAvoidService;

/**
 * 回避专家Controller
 * @author jacky
 * @version 2018-01-27
 */
@Controller
@RequestMapping(value = "${adminPath}/project/knotExpertAvoid")
public class KnotExpertAvoidController extends BaseController {

	@Autowired
	private KnotExpertAvoidService knotExpertAvoidService;
	
	@ModelAttribute
	public KnotExpertAvoid get(@RequestParam(required=false) String id) {
		KnotExpertAvoid entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = knotExpertAvoidService.get(id);
		}
		if (entity == null){
			entity = new KnotExpertAvoid();
		}
		return entity;
	}
	
	/**
	 * 回避专家列表页面
	 */
	@RequestMapping(value = {"list", ""})
	public String list(KnotExpertAvoid knotExpertAvoid, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KnotExpertAvoid> page = knotExpertAvoidService.findPage(new Page<KnotExpertAvoid>(request, response), knotExpertAvoid); 
		model.addAttribute("page", page);
		model.addAttribute("knotExpertAvoid", knotExpertAvoid);
		return "modules/project/knot/knotExpertAvoidList";
	}

	/**
	 * 查看，增加，编辑回避专家表单页面
	 */
	@RequiresPermissions(value={"project:knotExpertAvoid:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(KnotExpertAvoid knotExpertAvoid, Model model) {
		model.addAttribute("knotExpertAvoid", knotExpertAvoid);
		return "modules/project/knot/knotExpertAvoidForm";
	}

	/**
	 * 保存回避专家
	 */
	@RequiresPermissions(value={"project:knotExpertAvoid:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(KnotExpertAvoid knotExpertAvoid, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, knotExpertAvoid)){
			return form(knotExpertAvoid, model);
		}
		if(!knotExpertAvoid.getIsNewRecord()){//编辑表单保存
			KnotExpertAvoid t = knotExpertAvoidService.get(knotExpertAvoid.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(knotExpertAvoid, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			knotExpertAvoidService.save(t);//保存
		}else{//新增表单保存
			knotExpertAvoidService.save(knotExpertAvoid);//保存
		}
		addMessage(redirectAttributes, "保存回避专家成功");
		return "forward:"+Global.getAdminPath()+"/project/knotExpertAvoid/?repage";
	}
	
	/**
	 * 删除回避专家
	 */
	@RequiresPermissions("project:knotExpertAvoid:edit")
	@RequestMapping(value = "delete")
	public String delete(KnotExpertAvoid knotExpertAvoid, RedirectAttributes redirectAttributes) {
		knotExpertAvoidService.delete(knotExpertAvoid);
		addMessage(redirectAttributes, "删除回避专家成功");
		return "forward:"+Global.getAdminPath()+"/project/knotExpertAvoid/?repage";
	}
	
	/**
	 * 批量删除回避专家
	 */
	@RequiresPermissions("project:knotExpertAvoid:edit")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			knotExpertAvoidService.delete(knotExpertAvoidService.get(id));
		}
		addMessage(redirectAttributes, "删除回避专家成功");
		return "forward:"+Global.getAdminPath()+"/project/knotExpertAvoid/?repage";
	}
	

}